package com.neusoft.bizcore.auth.stress;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.enums.AuthPermissionType;
import com.neusoft.bizcore.auth.model.AuthPermission;
import com.neusoft.bizcore.auth.model.AuthRole;
import com.neusoft.bizcore.auth.repository.AuthPermissionRepository;
import com.neusoft.bizcore.auth.repository.AuthRoleRepository;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Profile("stress")
@Component
public class InitialStressData implements ApplicationListener<AuthFixtureEvent> {
    @Autowired
    private AuthRoleRepository authRoleRepository;
    @Autowired
    private AuthPermissionRepository authPermissionRepository;

    @Override
    public void onApplicationEvent(final AuthFixtureEvent event) {
        this.init();
    }

    public void init() {
        this.initRole();
        this.initPerm();
    }

    // 创建500个菜单权限，1000个按钮权限
    private void initPerm() {

        final List<AuthPermission> all = this.authPermissionRepository.findAll();

        if ((null != all) && (all.size() > 0)) {
            return;
        }

        final List<AuthPermission> perms = new ArrayList<>();

        perms.add(AuthPermission.builder().name("工单管理").code("menu_dcam_workorder").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("我的工单").code("menu_dcam_myWorkorder").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("待办工单").code("menu_dcam_todoWorkOrder").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("已办工单").code("menu_dcam_doneWorkOrder").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("资产管理").code("menu_dcam_fixed").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("资产维护").code("menu_dcam_fixedAssetMaintenance")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("资产盘点").code("menu_dcam_inventory").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("盘点计划").code("menu_dcam_inventoryPlan").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("任务安排").code("menu_dcam_inventoryDuty").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("任务执行信息").code("menu_dcam_inventoryExecuteResult")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("任务执行").code("menu_dcam_inventoryExecute").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("任务确认").code("menu_dcam_inventoryResult").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("维保管理").code("menu_dcam_maintenance").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("维保变更").code("menu_dcam_maintenanceChange")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("提醒设置").code("menu_dcam_maintenanceReminder")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("供应商档案").code("menu_dcam_manufacturer").type(AuthPermissionType.menu)
                .build());
        perms.add(
                AuthPermission.builder().name("合同档案").code("menu_dcam_contract").type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("产品档案").code("menu_dcam_product").type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("到货验收").code("menu_dcam_arrival").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("领用流程").code("menu_dcam_recipient").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("借用流程").code("menu_dcam_borrow").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("调拨流程").code("menu_dcam_allocatesupplies").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("维修流程").code("menu_dcam_repair").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("退库流程").code("menu_dcam_stockBack").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("归还流程").code("menu_dcam_back").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("变更使用人流程").code("menu_dcam_changeuser").type(AuthPermissionType.menu)
                .build());
        perms.add(
                AuthPermission.builder().name("处置流程").code("menu_dcam_dispose").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("备品备件").code("menu_dcam_spare").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("库存管理").code("menu_dcam_stock").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("库存查询").code("menu_dcam_stockQuery").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("仓库设置").code("menu_dcam_storehouse").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("分类管理").code("menu_dcam_spareType").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("货品档案").code("menu_dcam_goodsFile").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("基础设置").code("menu_dcam_base").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("资产分类").code("menu_dcam_assetType").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("位置管理").code("menu_dcam_locations").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("编码规则").code("menu_dcam_assetCodeRule").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("属性扩展").code("menu_dcam_assetExtFieldConfig")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("流程模板").code("menu_dcam_businessProcessType")
                .type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("系统设置").code("menu_dcam_settings").type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("定时任务").code("menu_dcam_sysTask").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("字典类型").code("menu_dcam_dictionaryType").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("字典管理").code("menu_dcam_dictionary").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("通知记录").code("menu_dcam_noticeRecord").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("表单管理").code("menu_dcam_workflowForm").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("BPMN").code("menu_dcam_workflowEdit").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("序列号模板").code("menu_dcam_serialNumber").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("日志管理").code("menu_dcam_logs").type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("日志查询").code("menu_dcam_logQuery").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("认证系统").code("menu_auth_parent").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("用户管理").code("menu_auth_users").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("角色管理").code("menu_auth_roles").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("部门管理").code("menu_auth_organizations").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("权限管理").code("menu_auth_permissions").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("钉钉应用配置").code("menu_auth_dingdingconfig").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("OAuth2认证配置").code("menu_auth_oauth2config")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("计划协调").code("menu_dcom_planCoordination").type(AuthPermissionType.menu)
                .build());
        perms.add(
                AuthPermission.builder().name("事件管理").code("menu_dcom_incident").type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("问题管理").code("menu_dcom_problem").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("变更管理").code("menu_dcom_change").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("工单管理").code("menu_dcom_workorder").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("我的工单").code("menu_dcom_myWorkOrder").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("待办工单").code("menu_dcom_todoWorkOrder").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("已办工单").code("menu_dcom_doneWorkOrder").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("例行巡检").code("menu_dcom_inspection").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("巡检计划").code("menu_dcom_inspectionRule").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("巡检统计").code("menu_dcom_inspectionTaskStatistics")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("巡检任务").code("menu_dcom_inspectionTask").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("运行维护").code("menu_dcom_maintainManager").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("维护规则").code("menu_dcom_maintainRule").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("维护计划").code("menu_dcom_maintainPlan").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("维护统计").code("menu_dcom_maintainTaskQuery")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("临时维护").code("menu_dcom_tmpMaintainTask").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("维护通知").code("menu_dcom_stopServiceNotice")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("维护任务").code("menu_dcom_maintainTask").type(AuthPermissionType.menu)
                .build());
        perms.add(
                AuthPermission.builder().name("知识库").code("menu_dcom_knowledge").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("类型配置").code("menu_dcom_knowledgeBaseConfiguration")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("知识维护").code("menu_dcom_knowledgeView").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("知识检索").code("menu_dcom_knowledgeComment").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("值班管理").code("menu_dcom_duty").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("职能管理").code("menu_dcom_functionalgroups").type(AuthPermissionType.menu)
                .build());
        perms.add(
                AuthPermission.builder().name("班次管理").code("menu_dcom_classes").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("班组管理").code("menu_dcom_teamgroup").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("值班规则管理").code("menu_dcom_schedulingrule").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("值班安排").code("menu_dcom_dutyArrangement").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("值班日历").code("menu_dcom_dutyCalender").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("我的日历").code("menu_dcom_personalCalender").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("交接班管理").code("menu_dcom_handover").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("换班管理").code("menu_dcom_shift").type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("节假日管理").code("menu_dcom_holiday").type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("配置管理").code("menu_dcom_setting").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("字典类型").code("menu_dcom_dictionaryType").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("字典管理").code("menu_dcom_dictionary").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("表单管理").code("menu_dcom_workflowForm").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("流程模板").code("menu_dcom_businessProcessType")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("BPMN").code("menu_dcom_bpmn").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("优先级管理").code("menu_dcom_priority").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("资源监测").code("menu_dcim").type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("资源列表").code("menu_dcim_resource").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("业务资源列表").code("menu_dcim_businessResource")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警管理").code("menu_dcim_alarm").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警摘要").code("menu_dcim_alarmAbstract").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("活动告警").code("menu_dcim_activeAlarm").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("历史告警").code("menu_dcim_historyAlarm").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("屏蔽告警").code("menu_dcim_shieldingAlarm").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("能效管理").code("menu_dcim_energy").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("能效摘要").code("menu_dcim_energyAbstract").type(AuthPermissionType.menu)
                .build());
        perms.add(
                AuthPermission.builder().name("容量管理").code("menu_dcim_capacity").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("容量摘要").code("menu_dcim_capacityAbstract").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("报表管理").code("menu_dcim_report").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("全测点报表").code("menu_dcim_reportAllSight").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("告警设置").code("menu_dcim_alarmSetting").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("告警标准化设置").code("menu_dcim_alarmStandardiseSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警级别设置").code("menu_dcim_alarmLevelSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警延时策略").code("menu_dcim_alarmDelayedSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警屏蔽策略").code("menu_dcim_alarmShieldingSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警工程预约策略").code("menu_dcim_alarmProjectBookingSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警类型策略").code("menu_dcim_alarmTypeSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警资源策略").code("menu_dcim_alarmResourceSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警通知规则").code("menu_dcim_alarmInformRuleSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警通知规则模板").code("menu_dcim_alarmInformRuleTemplateSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("告警短信网关设置").code("menu_dcim_alarmMessageGatewaySetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("分析设置").code("menu_dcim_analysisSetting").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("分析类别").code("menu_dcim_analysisTypeSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("分析公式").code("menu_dcim_analysisFormulaSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("分析因子").code("menu_dcim_analysisFactorSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("分析结果").code("menu_dcim_analysisResultSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("拓扑设置").code("menu_dcim_topologySetting").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("业务拓扑").code("menu_dcim_businessTopologySetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("监测设置").code("menu_dcim_monitorSetting").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("资源类别").code("menu_dcim_resourceTypeSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("监测指标").code("menu_dcim_monitorMetricSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("网关管理").code("menu_dcim_gatewaySetting").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("采集指标").code("menu_dcim_metricSetting").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("采集指标组").code("menu_dcim_metricGroupSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("资源监测设置").code("menu_dcim_monitorResourceSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("采集任务").code("menu_dcim_monitorJobSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("采集数据").code("menu_dcim_monitorDataSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("采集连接池").code("menu_dcim_monitorConnectionPoolSetting")
                .type(AuthPermissionType.menu).build());
        perms.add(
                AuthPermission.builder().name("系统设置").code("menu_dcim_setting").type(AuthPermissionType.menu).build());
        perms.add(AuthPermission.builder().name("字典类型").code("menu_dcim_dictionaryType").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("字典管理").code("menu_dcim_dictionary").type(AuthPermissionType.menu)
                .build());
        perms.add(
                AuthPermission.builder().name("定时任务").code("menu_dcim_sysTask").type(AuthPermissionType.menu).build());

        perms.add(AuthPermission.builder().name("配置库").code("menu_cmdb_home").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("实例搜索").code("menu_cmdb_outline").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("模型概要").code("menu_cmdb_categories").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("分类管理").code("menu_cmdb_schemas").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("模型管理").code("menu_cmdb_instances").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("实例管理").code("menu_cmdb_relationtypes").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("关联类型").code("menu_cmdb_relainsts").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("关联关系").code("menu_cmdb_businessemgr").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("业务管理").code("menu_cmdb_bizdef").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("业务模型管理").code("menu_cmdb_bizs").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("基础设施管理").code("menu_databus_gateway").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("网关管理").code("menu_databus_resources").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("设备管理").code("menu_databus_metircGroup").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("指标组管理").code("menu_databus_metirc").type(AuthPermissionType.menu)
                .build());
        perms.add(
                AuthPermission.builder().name("数据采集管理").code("menu_databus_jobManagement").type(AuthPermissionType.menu)
                        .build());
        perms.add(AuthPermission.builder().name("任务管理").code("menu_databus_monitorData").type(AuthPermissionType.menu)
                .build());
        perms.add(AuthPermission.builder().name("运维支撑管理").code("menu_databus_connectionPool")
                .type(AuthPermissionType.menu)
                .build());

        this.authPermissionRepository.saveAll(perms);

        InitialStressData.log.info(">>>>>>>>>>>>>>>init permission end<<<<<<<<<<<<<<<");

    }

    private void initRole() {

        final AuthRole tmp = this.authRoleRepository.findByCode("roleDemo");
        if (null != tmp) {
            return;
        }

        InitialStressData.log.info(">>>>>>>>>>>>>>>init role begin<<<<<<<<<<<<<<<");
        final AuthRole role = new AuthRole();
        role.setCode("roleDemo");
        role.setName("演示角色");
        role.setPermissions(new ArrayList<>());
        this.authRoleRepository.save(role);
        InitialStressData.log.info(">>>>>>>>>>>>>>>init role end<<<<<<<<<<<<<<<");

    }

}
